<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script type="text/javascript">
    function cart_read(cookieName){
        var search = cookieName + "=";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                end = document.cookie.indexOf(";", offset);
                if (end == -1){
                    end = document.cookie.length;
                    return unescape(document.cookie.substring(offset, end));
                }
            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    //添加商品至购物车
    function cart_add(item_no,item_name,item_amount,item_price){
        var cookieString=document.cookie;
        if(isNaN(item_amount)||item_amount<1||item_amount.indexOf('.')!=-1){
            alert("数量输入错误！");
        }else{
            var mer_list=cart_read('petInfo');
            var today = new Date();
            var expires = new Date();
            expires.setTime(today.getTime() + 1000*60*60*24*30);     //有效期为30天
            var item_detail="|"+item_no+"&"+item_name+"&"+item_amount+"&"+item_price;
            if(mer_list==false){
                document.cookie="petInfo="+escape(item_detail)+";expires=" + expires.toGMTString();
                alert("“"+item_name+"”\n"+"已经加入您的订单！");
            }else{
                document.cookie="petInfo="+mer_list+escape(item_detail)+";expires=" + expires.toGMTString();
                alert("“"+item_name+"”\n"+"已经加入您的订单！");
            }
            window.location.href="cart.html";
        }
    }
</script>

<table width="30%" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="46%">商品名称</td>
        <td width="14%">单价</td>
        <td width="20%">数量</td>
        <td width="20%">操作</td>
    </tr>
    <tr>
        <td>JavaScript从入门到精通</td>
        <td>50</td>
        <td><input type="text" id="sl1" size="4" value="1" maxlength="5" ></td>
        <td><a href="javascript:;" onClick="cart_add('goods1','JavaScript从入门到精通',sl1.value,'50')">添加</a></td>
    </tr>
    <tr>
        <td>联想笔记本电脑</td>
        <td>3600</td>
        <td><input type="text" id="sl2" size="4" value="1" maxlength="5" ></td>
        <td><a href="javascript:;" onClick="cart_add('goods2','联想笔记本电脑',sl2.value,'3600')">添加</a></td>
    </tr>
</table>

</body>
</html>